package com.archerdu.apply.controller.admin;

import java.io.IOException;
import java.io.Writer;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.archerdu.apply.model.Menu;
import com.archerdu.apply.model.User;
import com.archerdu.apply.service.MenuManager;
import com.archerdu.base.controller.BaseController;
import com.archerdu.commons.constant.DataConstant;

/**
 * 
 * @projectName: springDemo
 * @package: com.archerdu.apply.controller.admin
 * @className: MenuController
 * @description: 目录管理
 * @author: archer du
 * @createDate: 2013-8-9 下午4:57:38
 * @updateUser: dxf
 * @updateDate: 2013-8-9 下午4:57:38
 * @updateRemark: 新建
 * @version: 0.0.1
 */
@Controller
@Scope("prototype")
@RequestMapping("/admin/menu")
public class MenuController extends BaseController {

    private static final long serialVersionUID = 5676515795441813282L;

    private MenuManager       menuManager;

    private Menu              menu;

    @RequestMapping(value = "/add")
    public ModelAndView add(HttpServletRequest requ, MenuController mc) {
        menu = mc.getMenu();
        ModelAndView mv = new ModelAndView();
        mv.setViewName("admin/menu/add");
        if ("get".equalsIgnoreCase(requ.getMethod())) {
            mv.addObject(menu);
        } else {
            try {
                menu.setSort(0);
                menu.setCreateUser(((User) requ.getSession().getAttribute(DataConstant.SESSION_SYS_USER)).getId());
                menu.setCreatetime(getSqlDate());
                menuManager.add(menu);
                addMessage(DataConstant.COMMON_ADD_SUCCESS);
            } catch (Exception e) {
                addMessage(DataConstant.COMMON_ADD_FAILED);
                logger.error(e.getMessage(), e);
            }
        }
        return mv;
    }

    /**
     * 目录编辑
     * 
     * @param requ
     * @param menu
     * @return
     */
    @RequestMapping(value = "/edit")
    public ModelAndView edit(HttpServletRequest requ, MenuController mc) {

        logger.debug("menu.id is " + mc.getMenu().getId());
        menu = mc.getMenu();

        if ("get".equalsIgnoreCase(requ.getMethod())) {
            if (!StringUtils.isEmpty(menu.getId())) {
                menu = menuManager.get(menu.getId());
            }
        } else {
            menu.setUpdateUser(((User) requ.getSession().getAttribute((DataConstant.SESSION_SYS_USER))).getId());
            menu.setUpdatetime(getSqlDate());
            try {
                menuManager.modify(menu);
                menu = menuManager.get(menu.getId());
                addMessage(DataConstant.COMMON_EDIT_SUCCESS);
            } catch (Exception e) {
                addMessage(DataConstant.COMMON_EDIT_FAILED);
                logger.error(e.getMessage(), e);
            }
        }

        ModelAndView mav = new ModelAndView();
        mav.addObject(getMessage());
        mav.addObject(menu).setViewName("admin/menu/edit");
        return mav;
    }

    /**
     * 生成json格式的目录树结构
     * 
     * @param resp
     * @param writer
     */
    @RequestMapping(value = "/json")
    public void json(HttpServletResponse resp, Writer writer) {
        resp.setContentType("text/plain");
        resp.setCharacterEncoding("utf-8");
        try {
            Menu menu = new Menu();
            menu.setId(0);
            writer.write(menuManager.loadJson(menu));
        } catch (IOException e) {
            logger.error(e.getMessage(), e);
        }
    }

    /**
     * 指向目录树视图
     * 
     * @return
     */
    @RequestMapping(value = "tree")
    public String tree() {
        return ("admin/menu/tree");
    }

    public MenuManager getMenuManager() {
        return menuManager;
    }

    public void setMenuManager(MenuManager menuManager) {
        this.menuManager = menuManager;
    }

    public Menu getMenu() {
        return menu;
    }

    public void setMenu(Menu menu) {
        this.menu = menu;
    }
}
